//
//  HYSuspendButton.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/9.
//  

#import "HYSuspendButton.h"

@interface HYSuspendButton ()
{
    CGPoint beginPoint;
}

@property (nonatomic, strong) UIButton *enterButton;


@property (nonatomic, copy) dispatch_block_t clickBlock;

@property (nonatomic, assign) CGFloat margin;

@end

@implementation HYSuspendButton


- (instancetype)initWithClickBlock:(dispatch_block_t)clickBlock
                            margin:(CGFloat)margin
{
    self = [super init];
    if (self) {
        self.clickBlock = clickBlock;
        self.margin = margin;
        UIButton *enterButton = [UIButton new];
        enterButton.userInteractionEnabled = false;
        enterButton.backgroundColor = APPMainThemeColor;
        enterButton.titleLabel.font = [UIFont systemFontOfSize:14];
        enterButton.titleLabel.adjustsFontSizeToFitWidth = YES;
        enterButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [enterButton.titleLabel sizeToFit];
        [enterButton setTitleColor:APPTextThemeColor forState:UIControlStateNormal];
        [self addSubview:enterButton];
        [enterButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        self.enterButton = enterButton;
        
        // 单击的 Recognizer
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapToDo:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    [self.enterButton setTitle:title forState:UIControlStateNormal];
}

- (void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    [self.enterButton setTitleColor:titleColor forState:UIControlStateNormal];
}

- (void)setImage:(UIImage *)image
{
    [self.enterButton setImage:image forState:UIControlStateNormal];
}

- (void)singleTapToDo:(UITapGestureRecognizer *)sender
{
    if(self.clickBlock){
        self.clickBlock();
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:self];
    beginPoint = curPoint;
    [[self superview] bringSubviewToFront:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint nowPoint = [touch locationInView:self];
    
    float offsetX = nowPoint.x - beginPoint.x;
    float offsetY = nowPoint.y - beginPoint.y;
    
    CGFloat endSetX = self.center.x + offsetX;
    CGFloat endSetY = self.center.y + offsetY;
    // 设置悬浮窗口拖动区域，防止滑出页面
    if (endSetX < 40) {
        endSetX = 40;
    }else if (endSetX > kMainScreenWidth - 40) {
        endSetX = kMainScreenWidth - 40;
    }
    if (endSetY < STATUS_BAR_HEIGHT + 25) {
        endSetY = STATUS_BAR_HEIGHT + 25;
    }else if (endSetY > kMainScreenHeight - TABLE_BAR_HEIGHT - 40) {
        endSetY = kMainScreenHeight - TABLE_BAR_HEIGHT - 40;
    }
    self.center = CGPointMake(endSetX, endSetY);
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self location];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self location];
}

- (void)location {
    CGPoint point = self.center;
    if (point.x>[self superview].frame.size.width/2.0) {
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = CGRectMake([self superview].frame.size.width-self.frame.size.width-self.margin, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = CGRectMake(self.margin, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
        }];
    }
}

@end
